﻿using System;
using System.Collections.Generic;
using System.Configuration;

using ApplicationBoilerplate.DependencyInjection;

using mvcForum.Core.Interfaces.Search;
using mvcForum.SearchProvider.Lucene;
using mvcForum.SearchProvider.Lucene.Indexes;

namespace MvcForum.Hosting.Azure {

	/// <summary>
	/// Dependency builder for storing Lucene indexes on Azure.
	/// </summary>
	public class SearchBuilder : LuceneBaseSearchBuilder {

		/// <summary>
		/// Configure method of the SearchBuilder class.
		/// Here we wire up everything needed for MVC Forum to store Lucene indexes in an Azure blob storage.
		/// </summary>
		/// <param name="container">The dependency container.</param>
		public override void Configure(IDependencyContainer container) {
			// The directory resolver will run as a singleton, so let's create it.
			// This is for Azure, we better get the name of the container from the regular web.config
			IDirectoryResolver dirResolver = new DirectoryResolver(ConfigurationManager.AppSettings["AzureLuceneCatalog"]);
			// Let's register the Azure Lucene resolver!
			container.RegisterSingleton<IDirectoryResolver>(dirResolver);

			base.Configure(container);
		}

		/// <summary>
		/// Method for validating all requirements for the dependency builder are present.
		/// </summary>
		/// <param name="feedback">Return any warning/fatal feedback in this list.</param>
		public override void ValidateRequirements(IList<ApplicationRequirement> feedback) {
			// Get the bucket/catalogue!
			String container = ConfigurationManager.AppSettings["AzureLuceneCatalog"];
			// Is it empty?
			if (String.IsNullOrWhiteSpace(container)) {
				// This will make Lucene fail, let's return a warning!
				feedback.Add(new ApplicationRequirement { Level = RequirementLevel.Warning, Feedback = "You need an app setting for 'AzureLuceneCatalog' in the web.config file when using Azure's blob storage for Lucene storage!" });
			}
		}
	}
}